package com.wtgroup.demo.面试题.我的面试.华为OD笔试230627;

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Q3_求最大矿区 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别

        int[][] arr = new int[300][300];
        int r = 0;
        int colLen = 0;
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String line = in.nextLine();
            String[] split = line.split("");
            int c = 0;
            for (String s : split) {
                arr[r][c++] = Integer.parseInt(s);
            }
            colLen = c;
            r++;
        }
        int rowLen = r;

        int ans = 0;
        for (r = 0; r < rowLen; r++) {
            for (int c = 0; c < colLen; c++) {
                int ret = func(arr, r, c, rowLen, colLen);
                ans = Math.max(ans, ret);
            }
        }

        System.out.println(ans);
    }

    private static int func(int[][] arr, int row, int col, int rowLen, int colLen) {
        int currVal = arr[row][col];
        if (currVal == 0) {
            return 0;
        }
        int ret = currVal;
        arr[row][col] = 0;

        if (row - 1 >= 0 && arr[row - 1][col] > 0) {
            ret += func(arr, row - 1, col, rowLen, colLen);
        }
        if (row + 1 < rowLen && arr[row + 1][col] > 0) {
            ret += func(arr, row + 1, col, rowLen, colLen);
        }
        if (col - 1 >= 0 && arr[row][col - 1] > 0) {
            ret += func(arr, row, col - 1, rowLen, colLen);
        }
        if (col + 1 < colLen && arr[row][col + 1] > 0) {
            ret += func(arr, row, col + 1, rowLen, colLen);
        }

        return ret;
    }

}
